<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<?php
include_once($_SERVER['DOCUMENT_ROOT'] . "/htqlmba/ketnoiCSDL.php");
ob_start();
session_start();
$_SESSION['congsuat']=$_GET['congsuat'];
if(trim($_SESSION['congsuat'])=="") {$_SESSION['congsuat']='%';}
$_SESSION['donvi']=$_GET['donvi'];
$_SESSION['tram']=$_GET['tram'];
$_SESSION['tinhtrang']=$_GET['tinhtrang'];
?>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Kết quả tìm kiếm</title>

        <link href="../jquery-ui-1.10.3.custom/css/ui-lightness/style.css" rel="stylesheet" type="text/css" />

    </head>

    <body>
	


 <?php //kt co kq khong	
           
                    $cokq = 0;
               		$congsuat=trim($_GET['congsuat']);
					if($congsuat=="") $congsuat="%";
						
                    $from = "may_bien_ap a";
                    $where = "";
                    if ($_GET['donvi'] == "" && $_GET['tram'] == "" && $_GET['tinhtrang'] == "") {
                        $where.="CONG_SUAT like '" . $congsuat . "'";
                    } else if ($_GET['donvi'] == "" && $_GET['tram'] == "") {
                        $from.=",chi_tiet_tt_may b";
                        $where.="a.SO_N0=b.SO_N0 and CONG_SUAT like '" . $congsuat . "' and b.TEN_TINH_TRANG_MAY='" . $_GET['tinhtrang'] . "'";
                    } else if ($_GET['donvi'] == "" && $_GET['tinhtrang'] == "") {
                        $from.=",mba_tram b";
                        $where.="a.SO_N0=b.SO_N0 and CONG_SUAT like '" . $congsuat . "'and b.MA_TRAM like '" . $_GET['tram'] . "'";
                    } else if ($_GET['tram'] == "" && $_GET['tinhtrang'] == "") {
                        $from.=",chi_tiet_so_huu b";
                        $where.="a.SO_N0=b.SO_N0 and CONG_SUAT like '" . $congsuat . "'and b.MA_DON_VI like '" . $_GET['donvi'] . "'";
                    } else if ($_GET['donvi'] == "") {
                        $from.=",mba_tram b, chi_tiet_tt_may c";
                        $where.="a.SO_N0=b.SO_N0 and a.SO_N0=c.SO_N0 and CONG_SUAT like '" . $congsuat . "' and b.MA_TRAM like '" . $_GET['tram'] . "' and c.TEN_TINH_TRANG_MAY='" . $_GET['tinhtrang'] . "'";
                    } else if ($_GET['tram'] == "") {
                        $from.=",chi_tiet_so_huu b, chi_tiet_tt_may c";
                        $where.="a.SO_N0=b.SO_N0 and a.SO_N0=c.SO_N0 and CONG_SUAT like '" . $congsuat . "' and b.MA_DON_VI like '" . $_GET['donvi'] . "' and c.TEN_TINH_TRANG_MAY ='" . $_GET['tinhtrang'] . "'";
                    } else if ($_GET['tinhtrang'] == "") {
                        $from.=",chi_tiet_so_huu b, mba_tram c";
                        $where.="a.SO_N0=b.SO_N0 and a.SO_N0=c.SO_N0 and CONG_SUAT like '" . $congsuat . "' and b.MA_DON_VI like '" . $_GET['donvi'] . "' and c.MA_TRAM= '" . $_GET['tram'] . "'";
                    } else {
                        $from.=",chi_tiet_so_huu b, mba_tram c, chi_tiet_tt_may d";
                        $where.="a.SO_N0=b.SO_N0 and a.SO_N0=c.SO_N0 and a.SO_N0 = d.SO_N0 and CONG_SUAT like '" . $congsuat . "' and b.MA_DON_VI like '" . $_GET['donvi'] . "' and c.MA_TRAM like '" . $_GET['tram'] . "' and d.TEN_TINH_TRANG_MAY='" . $_GET['tinhtrang'] . "'";
                    }

                    $sql = "select distinct a.SO_N0,MSTS, e.CAP_DIEN_AP,TEN_LOAI_DAU,CONG_SUAT,CHIEU_DAI,CHIEU_RONG,CHIEU_CAO,DAU_MAY,RUOT_MAY,TONG,TEN_NSX,TEN_NUOC,THONG_SO_DO, NAM_SX, NAM_NHAP_VE,NHAN_XET,GHI_CHU from " . $from . ",loai_dien_ap e, nha_sx f where a.MA_DIEN_AP=e.MA_DIEN_AP and a.MA_NSX=f.MA_NSX and " . $where;

                    $result = mysql_query($sql, $link);

                    if (mysql_num_rows($result) != 0) {
                        while ($row = mysql_fetch_array($result)) {
                            $c1 = $row["SO_N0"];
                            $sql2 = "Select distinct a.MA_DON_VI, TEN_DON_VI from chi_tiet_so_huu a, don_vi b where a.MA_DON_VI=b.MA_DON_VI and a.SO_N0='" . $c1 . "' and NGAY_CHUYEN_DEN=(select max(NGAY_CHUYEN_DEN) from chi_tiet_so_huu where SO_N0='" . $c1 . "')";
                            $result2 = mysql_query($sql2, $link);
                            $row2 = mysql_fetch_array($result2);
                            $donvi = $row2["MA_DON_VI"];
                            //lay ten tram co ngay van hanh gan nhat				
                            $sql3 = "select distinct a.MA_TRAM, TEN_TRAM from mba_tram a, tram b where a.MA_TRAM=b.MA_TRAM and SO_N0='" . $c1 . "' and NGAY_VAN_HANH=(Select max(NGAY_VAN_HANH) from mba_tram where SO_N0='" . $c1 . "')";
                            $result3 = mysql_query($sql3, $link);
                            $row3 = mysql_fetch_array($result3);
                            $tram = $row3["MA_TRAM"];

                            //lay ten tinh trang gan day nhat
                            $sql0 = "select distinct TEN_TINH_TRANG_MAY from chi_tiet_tt_may where SO_N0='" . $c1 . "' and TINH_TRANG_NGAY=(select max(TINH_TRANG_NGAY) from chi_tiet_tt_may where SO_N0='" . $c1 . "')";
                            $result0 = mysql_query($sql0, $link);
                            if (mysql_num_rows($result0) != 0) {
                                $row0 = mysql_fetch_array($result0);
                                $tinhtrang = $row0["TEN_TINH_TRANG_MAY"];
                            }
                            //kiem tra don vi so huu hien tai cua mba
                            if ($_GET['donvi'] == "") {
                                if (($_GET['tram'] == "") || ($_GET['tram'] != "" && $tram == $_GET['tram'])) {
                                    if (($_GET['tinhtrang'] == "") || ($_GET['tinhtrang'] != "" && $tinhtrang == $_GET['tinhtrang'])) {
                                        $cokq = 1;
                                    }
                                }
                            } else if ($_GET['donvi'] != "") {

                                if (($_GET['tram'] == "" && $donvi == $_GET['donvi']) || ($_GET['tram'] != "" && $donvi == $_GET['donvi'] && $tram == $_GET['tram'])) {
                                    if (($_GET['tinhtrang'] == "") || ($_GET['tinhtrang'] != "" && $tinhtrang == $_GET['tinhtrang'])) {
                                        $cokq = 1;
                                    }
                                }
                            }
                        }
                    }

                    if ($cokq == 0)
                         header('Location: /htqlmba/search/kocokq.php');                 
             
            ?>
			
        <form name="kqtimnangcao" method="POST" >


            <p align="center" class="style1"><font color="red">THÔNG TIN CƠ BẢN </font></p>
            <?php
            $from = "may_bien_ap a";
            $where = "";
            if ($_GET['donvi'] == "" && $_GET['tram'] == "" && $_GET['tinhtrang'] == "") {
                $where.="CONG_SUAT like '" . $congsuat . "'";
            } else if ($_GET['donvi'] == "" && $_GET['tram'] == "") {
                $from.=",chi_tiet_tt_may b";
                $where.="a.SO_N0=b.SO_N0 and CONG_SUAT like '" . $congsuat . "' and b.TEN_TINH_TRANG_MAY='" . $_GET['tinhtrang'] . "'";
            } else if ($_GET['donvi'] == "" && $_GET['tinhtrang'] == "") {
                $from.=",mba_tram b";
                $where.="a.SO_N0=b.SO_N0 and CONG_SUAT like '" . $congsuat . "'and b.MA_TRAM like '" . $_GET['tram'] . "'";
            } else if ($_GET['tram'] == "" && $_GET['tinhtrang'] == "") {
                $from.=",chi_tiet_so_huu b";
                $where.="a.SO_N0=b.SO_N0 and CONG_SUAT like '" . $congsuat . "'and b.MA_DON_VI like '" . $_GET['donvi'] . "'";
            } else if ($_GET['donvi'] == "") {
                $from.=",mba_tram b, chi_tiet_tt_may c";
                $where.="a.SO_N0=b.SO_N0 and a.SO_N0=c.SO_N0 and CONG_SUAT like '" . $congsuat . "' and b.MA_TRAM like '" . $_GET['tram'] . "' and c.TEN_TINH_TRANG_MAY='" . $_GET['tinhtrang'] . "'";
            } else if ($_GET['tram'] == "") {
                $from.=",chi_tiet_so_huu b, chi_tiet_tt_may c";
                $where.="a.SO_N0=b.SO_N0 and a.SO_N0=c.SO_N0 and CONG_SUAT like '" . $congsuat . "' and b.MA_DON_VI like '" . $_GET['donvi'] . "' and c.TEN_TINH_TRANG_MAY ='" . $_GET['tinhtrang'] . "'";
            } else if ($_GET['tinhtrang'] == "") {
                $from.=",chi_tiet_so_huu b, mba_tram c";
                $where.="a.SO_N0=b.SO_N0 and a.SO_N0=c.SO_N0 and CONG_SUAT like '" . $congsuat . "' and b.MA_DON_VI like '" . $_GET['donvi'] . "' and c.MA_TRAM= '" . $_GET['tram'] . "'";
            } else {
                $from.=",chi_tiet_so_huu b, mba_tram c, chi_tiet_tt_may d";
                $where.="a.SO_N0=b.SO_N0 and a.SO_N0=c.SO_N0 and a.SO_N0 = d.SO_N0 and CONG_SUAT like '" . $congsuat . "' and b.MA_DON_VI like '" . $_GET['donvi'] . "' and c.MA_TRAM like '" . $_GET['tram'] . "' and d.TEN_TINH_TRANG_MAY='" . $_GET['tinhtrang'] . "'";
            }

            $sql = "select distinct a.SO_N0,MSTS, e.CAP_DIEN_AP,TEN_LOAI_DAU,CONG_SUAT,CHIEU_DAI,CHIEU_RONG,CHIEU_CAO,DAU_MAY,RUOT_MAY,TONG,TEN_NSX,TEN_NUOC,THONG_SO_DO, NAM_SX, NAM_NHAP_VE,NHAN_XET,GHI_CHU from " . $from . ",loai_dien_ap e, nha_sx f where a.MA_DIEN_AP=e.MA_DIEN_AP and a.MA_NSX=f.MA_NSX and " . $where;

            $result = mysql_query($sql, $link);
            ?>
            <table class='table table-condensed' width="1729" height="188" border="0" cellpadding="0" cellspacing="1">
                <tr>
                    <td width="35"  class="TableHeader"><span>STT</span></td>
                    <td width="34" class="TableHeader"><div align="center"><strong>Số N0</strong></div></td>
                    <td width="50" height="66" class="TableHeader"><div align="center" class="style4 style3">
                            <div align="center"><strong>MSTS</strong></div>
                        </div></td>
                    <td width="48" class="TableHeader"><div align="center" class="style4 style3"><strong>Loại điện áp (U)</strong></div></td>
                    <td width="55" class="TableHeader"><div align="center" class="style4 style3"><strong>Công suất (KVA)</strong></div></td>
                    <td width="57" class="TableHeader"><div align="center" class="style4 style3"><strong>Chiều dài<br />
                                (mm)</strong></div></td>
                    <td width="57" class="TableHeader"><div align="center"><span class="style4 style3"><strong>Chiều rộng<br />
                                    (mm)</strong></span></div></td>
                    <td width="74" class="TableHeader"><div align="center" class="style4 style3"><strong>Chiều cao(mm) </strong></div></td>
                    <td width="45" class="TableHeader"><div align="center" class="style4 style3"><strong>Đầu máy<br />
                                (kg)</strong></div></td>
                    <td width="49" class="TableHeader"><div align="center" class="style4 style3"><strong>Ruột máy<br />
                                (kg)</strong></div></td>
                    <td width="48" class="TableHeader"><div align="center" class="style4 style3"><strong>Tổng<br />
                                (kg)</strong></div></td>
                    <td width="54" class="TableHeader"><div align="center" class="style4 style3"><strong>Hãng sản xuất</strong></div></td>
                    <td width="73" class="TableHeader"><div align="center" class="style4 style3"><strong>N&#432;&#7899;c <br />
                                s&#7843;n xu&#7845;t</strong></div></td>
                    <td width="54" height="66" class="TableHeader"><div align="center" class="style4 style3"><strong>Lo&#7841;i d&#7847;u </strong></div></td>
                    <td width="72" height="66" class="TableHeader"><div align="center" class="style4 style3"><strong>Th&ocirc;ng s&#7889; &#273;o </strong></div></td>
                    <td width="68" class="TableHeader"><div align="center"><span class="style3"><strong>N&#259;m s&#7843;n xu&#7845;t </strong></span></div></td>
                    <td width="70" class="TableHeader"><div align="center" class="style3"><strong>Năm nhập về</strong> </div></td>
                    <td width="78" class="TableHeader"><div align="center" class="style3"><span>Đơn vị hiện đang sở hữu </span></div></td>
                    <td width="112" class="TableHeader"><div align="center" class="style3"><span>Trạm</span></div></td>
                    <td width="96" class="TableHeader"><div align="center" class="style3">Ngày vận hành </div></td>
                    <td width="106" class="TableHeader"><div align="center" class="style3"><span>Tình trạng máy </span></div></td>
                    <td width="99" height="66" class="TableHeader"><div align="center" class="style4 style3"><strong>Nh&#7853;n x&eacute;t </strong></div></td>
                    <td width="153" class="TableHeader"><div align="center" class="style4 style3"><strong>Ghi ch&uacute;</strong></div></td>
                </tr>
                <?php
                if (mysql_num_rows($result) != 0) {
                    $stt = 0;
                    while ($row = mysql_fetch_array($result)) {
                        $c1 = $row["SO_N0"];
                        $c2 = $row["MSTS"];
                        $c3 = $row["CAP_DIEN_AP"];
                        $c4 = $row["CONG_SUAT"];
                        $c5 = $row["CHIEU_DAI"];
                        $c6 = $row["CHIEU_RONG"];
                        $c7 = $row["CHIEU_CAO"];
                        $c8 = $row["DAU_MAY"];
                        $c9 = $row["RUOT_MAY"];
                        $c10 = $row["TONG"];
                        $c11 = $row["TEN_NSX"];
                        $c12 = $row["TEN_NUOC"];
                        $c13 = $row["TEN_LOAI_DAU"];
                        $c14 = $row["THONG_SO_DO"];
                        $c15 = $row["NAM_SX"];
                        $c16 = $row["NAM_NHAP_VE"];
                        $c17 = "";
                        $c18 = "";
                        $c19 = "";
                        $c20 = $row["NHAN_XET"];
                        $c21 = $row["GHI_CHU"];
                        $c22 = "";
                        //Lay ten don vi co ngay chuyen den gan nhat		
                        $sql2 = "Select distinct a.MA_DON_VI, TEN_DON_VI from chi_tiet_so_huu a, don_vi b where a.MA_DON_VI=b.MA_DON_VI and a.SO_N0='" . $c1 . "' and NGAY_CHUYEN_DEN=(select max(NGAY_CHUYEN_DEN) from chi_tiet_so_huu where SO_N0='" . $c1 . "')";
                        $result2 = mysql_query($sql2, $link);
                        $row2 = mysql_fetch_array($result2);
                        $c17.=$row2["TEN_DON_VI"];
                        $donvi = $row2["MA_DON_VI"];
                        //lay ten tram co ngay van hanh gan nhat				
                        $sql3 = "select distinct a.MA_TRAM, TEN_TRAM,NGAY_VAN_HANH from mba_tram a, tram b where a.MA_TRAM=b.MA_TRAM and SO_N0='" . $c1 . "' and NGAY_VAN_HANH=(Select max(NGAY_VAN_HANH) from mba_tram where SO_N0='" . $c1 . "')";
                        $result3 = mysql_query($sql3, $link);
                        $row3 = mysql_fetch_array($result3);
                        $c18.=$row3["TEN_TRAM"];
                        $c22.=$row3["NGAY_VAN_HANH"];
                        $tram = $row3["MA_TRAM"];

                        //lay ten tinh trang gan day nhat
                        $sql0 = "select distinct TEN_TINH_TRANG_MAY from chi_tiet_tt_may where SO_N0='" . $c1 . "' and TINH_TRANG_NGAY=(select max(TINH_TRANG_NGAY) from chi_tiet_tt_may where SO_N0='" . $c1 . "')";
                        $result0 = mysql_query($sql0, $link);
                        if (mysql_num_rows($result0) != 0) {
                            $row0 = mysql_fetch_array($result0);
                            $c19.=$row0["TEN_TINH_TRANG_MAY"];
                            $tinhtrang = $row0["TEN_TINH_TRANG_MAY"];
                        }
                        //kiem tra don vi so huu hien tai cua mba
                        if ($_GET['donvi'] == "") {
                            if (($_GET['tram'] == "") || ($_GET['tram'] != "" && $tram == $_GET['tram'])) {
                                if (($_GET['tinhtrang'] == "") || ($_GET['tinhtrang'] != "" && $tinhtrang == $_GET['tinhtrang'])) {
                                    $stt++;
                                    $tbcolor = "TableField";
                                    if ($stt % 2 == 0) {
                                        $tbcolor = "TableField2";
                                    }
                                    echo "<tr><td class='TableHeader'><b>$stt</b></td>";
                                    echo "<td class='" . $tbcolor . "'>$c1</td>";
                                    echo "<td class='" . $tbcolor . "'>$c2</td>";
                                    echo "<td class='" . $tbcolor . "'>$c3</td>";
                                    echo "<td class='" . $tbcolor . "'>$c4</td>";
                                    echo "<td class='" . $tbcolor . "'>$c5</td>";
                                    echo "<td class='" . $tbcolor . "'>$c6</td>";
                                    echo "<td class='" . $tbcolor . "'>$c7</td>";
                                    echo "<td class='" . $tbcolor . "'>$c8</td>";
                                    echo "<td class='" . $tbcolor . "'>$c9</td>";
                                    echo "<td class='" . $tbcolor . "'>$c10</td>";
                                    echo "<td class='" . $tbcolor . "'>$c11</td>";
                                    echo "<td class='" . $tbcolor . "'>$c12</td>";
                                    echo "<td class='" . $tbcolor . "'>$c13</td>";
                                    echo "<td class='" . $tbcolor . "'>$c14</td>";
                                    echo "<td class='" . $tbcolor . "'>$c15</td>";
                                    echo "<td class='" . $tbcolor . "'>$c16</td>";
                                    echo "<td class='" . $tbcolor . "'>$c17</td>";
                                    echo "<td class='" . $tbcolor . "'>$c18</td>";
                                    echo "<td class='" . $tbcolor . "'>$c22</td>";
                                    echo "<td class='" . $tbcolor . "'>$c19</td>";
                                    echo "<td class='" . $tbcolor . "'>$c20</td>";
                                    echo "<td class='" . $tbcolor . "'>$c21</td></tr>";
                                }
                            }
                        } else if ($_GET['donvi'] != "") {

                            if (($_GET['tram'] == "" && $donvi == $_GET['donvi']) || ($_GET['tram'] != "" && $donvi == $_GET['donvi'] && $tram == $_GET['tram'])) {
                                if (($_GET['tinhtrang'] == "") || ($_GET['tinhtrang'] != "" && $tinhtrang == $_GET['tinhtrang'])) {
                                    $stt++;
                                    $tbcolor = "TableField";
                                    if ($stt % 2 == 0) {
                                        $tbcolor = "TableField2";
                                    }
                                    echo "<tr><td class='TableHeader'><b>$stt</b></td>";
                                    echo "<td class='" . $tbcolor . "'>$c1</td>";
                                    echo "<td class='" . $tbcolor . "'>$c2</td>";
                                    echo "<td class='" . $tbcolor . "'>$c3</td>";
                                    echo "<td class='" . $tbcolor . "'>$c4</td>";
                                    echo "<td class='" . $tbcolor . "'>$c5</td>";
                                    echo "<td class='" . $tbcolor . "'>$c6</td>";
                                    echo "<td class='" . $tbcolor . "'>$c7</td>";
                                    echo "<td class='" . $tbcolor . "'>$c8</td>";
                                    echo "<td class='" . $tbcolor . "'>$c9</td>";
                                    echo "<td class='" . $tbcolor . "'>$c10</td>";
                                    echo "<td class='" . $tbcolor . "'>$c11</td>";
                                    echo "<td class='" . $tbcolor . "'>$c12</td>";
                                    echo "<td class='" . $tbcolor . "'>$c13</td>";
                                    echo "<td class='" . $tbcolor . "'>$c14</td>";
                                    echo "<td class='" . $tbcolor . "'>$c15</td>";
                                    echo "<td class='" . $tbcolor . "'>$c16</td>";
                                    echo "<td class='" . $tbcolor . "'>$c17</td>";
                                    echo "<td class='" . $tbcolor . "'>$c18</td>";
                                    echo "<td class='" . $tbcolor . "'>$c22</td>";
                                    echo "<td class='" . $tbcolor . "'>$c19</td>";
                                    echo "<td class='" . $tbcolor . "'>$c20</td>";
                                    echo "<td class='" . $tbcolor . "'>$c21</td></tr>";
                                }
                            }
                        }
                    }
                }
                ?>
            </table>


            <p align="center">&nbsp;</p>
            <p align="right">
                <input name='inexceltnc' type="submit"  value="In ra file Excel" />
            </p>

    </body>
</html>
